package com.ruoyi.charge.controller;

import com.ruoyi.charge.auth.service.LoginTokenService;
import com.ruoyi.charge.bo.StoreAddBo;
import com.ruoyi.charge.bo.StoreEditBo;
import com.ruoyi.charge.bo.StoreQueryBo;
import com.ruoyi.charge.service.IStoreService;
import com.ruoyi.charge.vo.StoreVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺管理Controller
 *
 * @author fanqie
 * @date 2021-10-25
 */
@Api(value = "店铺管理控制器", tags = {"店铺管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/charge/store")
public class StoreController extends BaseController {

    private final IStoreService iStoreService;
    private final LoginTokenService loginTokenService;

    /**
     * 查询店铺管理列表
     */
    @ApiOperation("查询店铺管理列表")
    @GetMapping("/list")
    public TableDataInfo<StoreVo> list(StoreQueryBo bo) {
        startPage();
        List<StoreVo> list = iStoreService.queryList(bo);
        return getDataTable(list);
    }


    /**
     * 获取店铺管理详细信息
     */
    @ApiOperation("获取店铺管理详细信息")
    @GetMapping("/{storeId}")
    public AjaxResult<StoreVo> getInfo(@PathVariable("storeId") Long storeId) {
        return AjaxResult.success(iStoreService.queryById(storeId));
    }

    /**
     * 新增店铺管理
     */
    @ApiOperation("新增店铺管理")
    @Log(title = "店铺管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody StoreAddBo bo) {
        return toAjax(iStoreService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改店铺管理
     */
    @ApiOperation("修改店铺管理")
    @Log(title = "店铺管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody StoreEditBo bo) {
        Long userId = loginTokenService.getLoginUserId(ServletUtils.getRequest());
        bo.setStoreId(userId);
        return toAjax(iStoreService.updateByEditBo(bo) ? 1 : 0);
    }

}
